package cby.plugin.check.resource

import cby.plugin.check.util.FileUtils
import cby.plugin.check.value.ValueChecker
import groovy.xml.XmlParser
import org.gradle.api.Plugin
import org.gradle.api.Project

class MainCheckPlugin implements Plugin<Project> {

    private static final String KEYWORD_DRAWABLE = "drawable"
    private static final String KEYWORD_LAYOUT = "layout"
    private static final String KEYWORD_MIPMAP = "mipmap"
    private static final String KEYWORD_RAW = "raw"
    private static final String KEYWORD_VALUE = "value"
    private static final String KEYWORD_XML = "xml"

    private Map<String, String> fileErrorMap = new HashMap<>()

    @Override
    void apply(Project project) {
        // 创建拓展属性
        CheckValueConfig valueConfig = project.extensions.create("valueConfig", CheckValueConfig)
        CheckResourceConfig resourceConfig = project.extensions.create("resourceConfig", CheckResourceConfig)
        CheckDefaultConfig defaultConfig = project.extensions.create("checkDefaultConfig", CheckDefaultConfig)

        println('CheckDefaultConfig -- namePrefix: '+defaultConfig.getNamePrefix())
        println('CheckDefaultConfig -- executeInAlone: '+defaultConfig.getExecuteInAlone())

        println('CheckResourceConfig -- namePrefix: '+resourceConfig.getNamePrefix())
        println('CheckResourceConfig -- ignoreDrawable: '+resourceConfig.getIgnoreDrawable())
        println('CheckResourceConfig -- ignoreLayout: '+resourceConfig.getIgnoreLayout())
        println('CheckResourceConfig -- ignoreMipmap: '+resourceConfig.getIgnoreMipmap())
        println('CheckResourceConfig -- ignoreRaw: '+resourceConfig.getIgnoreRaw())
        println('CheckResourceConfig -- ignoreValue: '+resourceConfig.getIgnoreValue())
        println('CheckResourceConfig -- ignoreXml: '+resourceConfig.getIgnoreXml())

        // 创建任务
        //CheckValueConfig valueConfig = project.getTasks().create("valueConfig", CheckValueConfig.class)
        project.task("checkRes") {

            doLast {
                println('start check value')
                // 检车资源是否规范命名
                ValueChecker.startCheckValue(project, valueConfig)


                new Thread(){
                    @Override
                    void run() {
                        checkResFile(project, resourceConfig)
                        if (!fileErrorMap.isEmpty()) {
                            println("res file info is error")
                            FileUtils.writeObject(fileErrorMap,"FileError.txt")
                        } else {
                            File file = new File("ResourcesError"+File.separator+"FileError.txt")
                            if(file.exists()){
                                file.delete()
                            }
                            println("res file info is ok")
                        }
                        fileErrorMap.clear()
                    }
                }.start()
            }
        }

    }

    /**
     * 检查res目录下的文件是否符合规范
     * @param project
     * @param prefix
     */
    void checkResFile(Project project, CheckResourceConfig resourceConfig) {
        if(project.childProjects.size() > 1) {
            project.childProjects.each {
                checkResFile(it.value, resourceConfig)
            }
        }else{
            File file = new File("${project.projectDir}/src/main/res")
            if(!file.exists()) {
                println("res 不存在")
                return
            }
            checkFile(file, resourceConfig)
        }

    }

    /**
     * 递归检查是文件
     * @param file
     * @param prefix
     */
    void checkFile(File file, CheckResourceConfig resourceConfig) {
        if(file == null || !file.exists()) {
            println("res = "+file)
            return
        }
        println("res path = "+file.getPath())
        if(checkResFileIgnore(resourceConfig, file)){
            def list = file.listFiles()
            list.each {
                checkFile(it, resourceConfig)
            }
        }else{
            if(!file.getName().startsWith(resourceConfig.getNamePrefix())) {
                fileErrorMap.put(file.getName(), file.getPath());
            }
        }
    }

    boolean checkResFileIgnore(CheckResourceConfig resourceConfig, File file){
        // 忽略drawable资源的检测
        if(resourceConfig.isIgnoreDrawable()
                && file.isDirectory()
                && file.getName().startsWith(KEYWORD_DRAWABLE)) {
            return false
        }

        // 忽略layout资源的检测
        if(resourceConfig.isIgnoreLayout()
                && file.isDirectory()
                && file.getName().startsWith(KEYWORD_LAYOUT)) {
            return false
        }

        // 忽略mipmap资源的检测
        if(resourceConfig.isIgnoreMipmap()
                && file.isDirectory()
                && file.getName().startsWith(KEYWORD_MIPMAP)) {
            return false
        }

        // 忽略raw资源的检测
        if(resourceConfig.isIgnoreRaw()
                && file.isDirectory()
                && file.getName().startsWith(KEYWORD_RAW)) {
            return false
        }

        // 忽略value资源的检测
        if(resourceConfig.isIgnoreValue()
                && file.isDirectory()
                && file.getName().startsWith(KEYWORD_VALUE)) {
            return false
        }

        // 忽略xml资源的检测
        if(resourceConfig.isIgnoreXml()
                && file.isDirectory()
                && file.getName().startsWith(KEYWORD_XML)) {
            return false
        }

        return file.isDirectory()
    }





}